$(function () {
    $('#tTime').datetimebox('setValue', formatterDate(new Date()));

    $('#returnCar').click(function () {
        initPopUp({title:'确定要还车么？',text:''},function () {
            var fromData = $('#ff').serialize();
            fromData = strToObj(fromData);
            if (!fromData.time) {
                window.top.sendErrorMsg('时间不能为空');
                return false;
            }
            $.ajax({
                url:baseUrl+'lm/car/saveConfirm/true',
                data:$('#ff').serialize(),
                type:'POST',
                success:function (data) {
                    location.reload();
                },
                error:function (data) {
                    window.top.sendErrorMsg(data['responseText']);
                }
            });
        });
    });

    $('#useCar').click(function () {
        initPopUp({title:'确定要取车么？',text:''},function () {
            var fromData = $('#ff').serialize();
            fromData = strToObj(fromData);
            if (!fromData.sDriverName) {
                window.top.sendErrorMsg('驾驶员不能为空');
                return false;
            }
            if (!fromData.iCarId) {
                window.top.sendErrorMsg('车辆不能为空');
                return false;
            }
            if (!fromData.time) {
                window.top.sendErrorMsg('时间不能为空');
                return false;
            }
            $.ajax({
                url:baseUrl+'lm/car/saveConfirm',
                data:$('#ff').serialize(),
                type:'POST',
                success:function (data) {
                    location.reload();
                },
                error:function (data) {
                    window.top.sendErrorMsg(data['responseText']);
                }
            });
        });
    });

});

function strToObj(str){
    str = str.replace(/&/g,"','");
    str = str.replace(/=/g,"':'");
    str = "({'"+str +"'})";
    return  eval(str);
}

formatterDate = function (date) {
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
        + (date.getMonth() + 1);
    var hor = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
};

initPopUp = function (_options,actFunc,disFunc) {
    var _default={
        title: '确认删除',
        text: '你确定要删除这条记录吗?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
    }
    if(!disFunc){
        disFunc=function (dismiss) {
            if(dismiss=='cancel'){
                return false;
            }
        }
    }
    var _options=$.extend({}, _default, _options);
    window.top.swal(_options).then(actFunc,disFunc);
}